﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using AbstractSql;

using MbUnit.Framework;

namespace UnitTests {
    [TestsOn(typeof(AbsDBConnection))]
    [TestFixture]
    public class AbsDBConnectionTests {
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void AddingDefaultConnection_ThatDoesntExist_ShouldThrowException() {
            AbsDBConnection.SetDefaultConnectionString("DoesntExist");
        }
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void AddingNamedConnection_ThatDoesntExist_ShouldThrowException() {
            AbsDBConnection.AddNamedConnectionString("DoesntExist");
        }
        [Test]
        [ExpectedException(typeof(Exception))]
        public void GetAbsDBCommandForNamed_WhenNotSet_ThrowsException() {
            IAbsDBCommand cmd = AbsDBConnection.GetAbsDBCommandFor("DoesntExist");
            cmd.SqlCommand("select 1").ExecuteNonQuery();
        }
        [Test]
        public void CanGetDBCommandForDefaultConnection() {
            AbsDBConnection.SetDefaultConnectionString("Test");
            IAbsDBCommand cmd = AbsDBConnection.GetAbsDBCommandForDefault();
            int value = cmd.SqlCommand("select ColInt from test where ColInt =1")
                .ExecuteScalar<int>();
            Assert.AreEqual(1, value);
        }
        [Test]
        public void CanGetDBCommandForNamedConnection() {
            AbsDBConnection.AddNamedConnectionString("Test");
            IAbsDBCommand cmd = AbsDBConnection.GetAbsDBCommandFor("Test");
            int value = cmd.SqlCommand("select ColInt from test where ColInt =1")
                .ExecuteScalar<int>();
            Assert.AreEqual(1, value);
        }
    }
}